﻿
CommonRegExp={
    uploadType:/\.(jpg|gif|png)$/i
}
Object.extend = function(destination, source) {
  for (var property in source)
    destination[property] = source[property];
  return destination;
};
function StringBuilder(value)
{
    this.strings = new Array();
    this.append(value);
}
StringBuilder.prototype = {
 length : 0,
 append : function (value)
 {
  if (value)
  {
   this.strings.push(value);
   this.length += value.length
  }
 },
 
 clear : function ()
 {
  this.strings.length = 1;
  this.length = 0;
 },
 
 toString : function ()
 {
     return this.strings.join("");
 }
}
    function getById(id){return document.getElementById(id);}

    function getQueryString(key){
       var querty_re = new RegExp("^[^\\?]*\\?(?:[^&]*&)*"+key+"=([^&]*)(?:[^&]*&?)*$","ig");
       
        if(querty_re.test(top.location.href))
            return top.location.href.replace(querty_re,"$1");
         return "";
    }


    //返回指定的URL参数,自动将URL解码
    function getQueryStr(key){
        return decodeURI(getQueryString(key));
    }
    
    
    function setQueryStr(key,value,url){
       // alert(url);
        if(!url)
            url=window.location.href;
        var result='';
        var re= new RegExp("(\\?|&|$)"+key+"=[^&]*(&|$)","i");  
        if(re.test(url))
            return url.replace(re,"$1"+key+"="+decodeURI(value)+"$2") 
        var strStart="?";     
        if(/\?[^=]*=[^&]*/i.test(url))
            strStart= "&";
        return url+strStart+key+"="+decodeURI(value);
    }
    
    


    //清空文件上传框
    function clearFileInput(file){
        var form=document.createElement('form');
        document.body.appendChild(form);
        //记住file在旧表单中的的位置
        var pos=file.nextSibling;
        form.appendChild(file);
        form.reset();
        pos.parentNode.insertBefore(file,pos);
        document.body.removeChild(form);
    }

    //创建html对象
    function createHtmlTag(para){
        var tag="";
        var regNoInner=/^input|img|hr|br$/i
        tag+="<"+para.tagName+" ";
        var attStr=''
        if(typeof para.tagAttributes=="object")
               for(var o in para.tagAttributes){
                    
                    attStr+=(/className/i.test(o)?'class':o)+"=\""+para.tagAttributes[o]+"\" ";
               }
       else if(typeof para.tagAttributes=="string") 
            attStr=para.tagAttributes;
              
       attStr=formatText(attStr,para.paraValues);
       tag+=' '+attStr;
       if(regNoInner.test(para.tagName))
            tag+="/>";
       else     tag+="></"+para.tagName+">";
       return tag;
       
    }
   //格式化字符串处理
    function formatText(text,valuePara){
        if(!valuePara)  
            return text;
        var re=/{#([^:}]+)(?::([^}]+))?}/ig
        var result=text;
        var match;
        while(match=re.exec(text)){
		    var replacerStr=valuePara[match[1]];
		    if(match[2]&&match[2].trim()!='')
		        replacerStr=eval('"'+replacerStr+'"'+'.'+match[2]);
		    result=result.replace(match[0],replacerStr);
		}
		return result;
    }
 
    function getBorderWidth(obj,borderType){
        var result='';
        if(borderType)
            result= $(obj).css(borderType);
        else
            result=$(obj).css('borderWidth');
        if(/^(\d+)/i.test(result))
            return result.replace(/^(\d+)\w*$/i,"$1");
        return 0;
    }
    
 

   Object.extend(String.prototype,{
        //去字符串的首尾空格
        trim:function (){
            return this.replace(/(^\s*)|(\s*$)/ig,"");
        },
        //返回字符串数组
        //return Array()
        toArray:function (){
            return this.split('');
        },
        //得到字符串的真实长度(中文2个字符)
        //returns:int
        len:function(){
            return this.replace(/[\u4e00-\u9fa5]/g,"aa").length;
        },
        //截取字符串
        //count,要截的数量
        //endStr:截完后用什么字符串来补
        //如"abcdefg".cutLeft(5,'...')=='ab...'
        cutLeft:function(count,endStr){
            if(!endStr) endStr='';
              var arr=this.toArray();
            var size=0,index=0;
            var result=this;
            for(var o in arr){
                if(/[\u4e00-\u9fa5]/g.test(arr[index]))
                   size+=2;
                else
                    size++;
                index++;
                if (size > count)
                {
                    result = this.substring(0, index - endStr.len()) + endStr;
                    break;
                }
                
                
            }
            return result;
        },
        htmlEncode:function (){
            var result=this;
            result=result.replace(">", "&gt;");
			result=result.replace("<", "&lt;");
			result=result.replace(" ", "&nbsp;");
			result=result.replace(" ", "&nbsp;");
			result=result.replace("\"", "&quot;");
			result=result.replace("\'", "&#39;");
			result=result.replace(/\n/ig, "<br/> ");
			return result;
        },
        htmlDiscode:function (){
            var result=this;
            result=result.replace("&gt;", ">");
			result=result.replace("&lt;", "<");
			result=result.replace("&nbsp;"," ");
			result=result.replace("&nbsp;"," ");
			result=result.replace("&quot;","\"");
			result=result.replace("&#39;","\'");
			result=result.replace(/<br\s*\/?\s*>/ig,"\n");
			return result;
        }
    })
 
//如果文本框有自定义属性 [blurMsg]则当它的值是空的时候，显示之，移入后清空
//blurClassName:默认字符的样式名称
function setInputMsg(blurClassName){
    $(":input[blurMsg]").blur(function (){
        if(this.value==''){
                this.value=this.getAttribute('blurMsg');
                $(this).addClass(blurClassName).attr("input_msg_empty","true");
           }
        }).focus(function (){
            if(this.value==this.getAttribute('blurMsg'))
            {
                this.value="";
                $(this).removeClass(blurClassName).attr("input_msg_empty","false");
            }
    }).blur();
}
//返回指输入框的值
function getInputValue(selecter){
    var obj = $(selecter)[0];
    if(obj.getAttribute('input_msg_empty')=='true')
        return '';
    return obj.value.trim();
}



$.fn.extend({
    //缩放图片
    zoomImage:function(resizeWidth,resizeHeight,callback){
        var result=this;
        var jqImg=this.filter('img');
        //onload
        jqImg.load(function (){
            
            var jqObj=$(this);
            if(jqObj.attr('isError')=='true')  jqObj.attr('alt','').attr('isError',null);
            
            var width=jqObj.width();
            var height=jqObj.height();  
            
            var objImagePreloader = new Image();
			objImagePreloader.onload = function() {
                             	    
			  width=objImagePreloader.width;
			  height=objImagePreloader.height;
			  
		       objImagePreloader.onload=function(){};
			};
			objImagePreloader.src = jqObj.attr('src');

            jqObj.css({width:width,height:height}).attr('width','').attr('height','').css('maxWidth',resizeWidth).css('maxHeight',resizeHeight);
         
                if((width>resizeWidth) &&(height>resizeHeight))
                {
                   if(width>height)
                   {
                        jqObj.width(resizeWidth);
		                jqObj.height(height*(resizeWidth/width)); 
                   }
                   else
                   {
                        jqObj.height(resizeHeight);
		                jqObj.width(width*(resizeHeight/height));
                   }
                }
                else
                {
                    if(width>resizeWidth)
	                {
		                jqObj.width(resizeWidth);
		                jqObj.height(height*(resizeWidth/width));
    		                		            
	                }
	                if(height>resizeHeight)
	                {
		                jqObj.height(resizeHeight);
		                jqObj.width(width*(resizeHeight/height));
    		            
	                }	
	            }
	            
            jqObj.css({width:jqObj.width(),height:jqObj.height()}).attr('width','').attr('height','').css('maxWidth',resizeWidth).css('maxHeight',resizeHeight);
	           
	        if(typeof callback=='function'){
	            callback();
	         
	        }
//	         $('span[photo_type=picDate]').html(width+'|'+height+"|------|"+jqObj.width()+"|"+jqObj.height());
	        
	                
        })
        jqImg.error(function (){
            this.setAttribute('isError','true');
            this.setAttribute('alt','没有找到该图片!');
            if(typeof callback=='function'){
	            callback();
	        }
        })  
        
    },
    //resizeImage参数说明
    //initCss:{width:100px;height:100px;}//初始化时的样式
    //resizeWidth:100       //限制的最大宽度
    //resizeHeight:100      //高度
    //imgSrc:'xxxx.jpg'     //图片地址
    resizeImage:function (options,callback){
        var resizeWidth=options.resizeWidth;
        var resizeHeight=options.resizeHeight
        var jqObj=$(this);
         if(options.imgSrc)
                jqObj.attr('src','').attr('src',options.imgSrc);
        if(options.initCss)         jqObj.css(options.initCss);
        jqObj.zoomImage(resizeWidth,resizeHeight,callback);
        
    },
    hasClass:function (className){
            var obj=this.filter(':first')
            if(obj.length==0)
                return false;
            var str='(\\s+|^)'+className+'(\\s+|$)';
//           alert(str);
//           return;
            var re=new RegExp(str,"i");
            return re.test(obj.attr('class'));
        }
});


//************************Begin Debug********************************
//body顶部插入一个调试信息的容器
function Debug(){
    
}
Object.extend(Debug.prototype,{
    tagName:'div',
    css:{},
    tagAttributes:'testID=debug_test style="text-align:center"',
    filter:"[testID=debug_test]",
    appendDebugMsg:function (msg){
      
        this.debugContainer().append(msg);
    },
    showDebugMsg:function (msg){
        this.debugContainer().html(msg)
    },
    //返回debug容器
    debugContainer:function (){
        var result=null;
        if($(this.filter).length==0){
            var result= this.createDebugContainer();
            result.prependTo('body');
           return result
        }
        else
            return $(this.filter)
    },
    createDebugContainer:function (){
        return $(createHtmlTag({
                    tagName:this.tagName,
                    tagAttributes:this.tagAttributes//,
                   // paraValues:null
                })
        ).css(this.css)
    }
        })

function QueryString() {
    var name, value, i;
    var str = location.href;
    var num = str.indexOf("?")
    str = str.substr(num + 1);
    var arrtmp = str.split("&");
    for (i = 0; i < arrtmp.length; i++) {
        num = arrtmp[i].indexOf("=");
        if (num > 0) {
            name = arrtmp[i].substring(0, num);
            value = arrtmp[i].substr(num + 1);
            this[name] = value;
        }
    }
}

//************************End  Debug********************************



        
//**************Begin 详细信息动画*************************************************
        //要弹出窗口的mouseover事件
        function plugin_mouseover(obj,para){
                    obj.onmouseover=function (){}
                 
                       var paraValues=new Object();
                       if(obj.getAttribute('paraValues'))
                           eval(' paraValues='+obj.getAttribute('paraValues'))
                   
                        var       forObject= $(createHtmlTag(para)).css(para.initCss).css({position:"absolute",zIndex:'1',overflow :'hidden',display:'inline'})//.attr("forover","hover_window_"+tempTar+"_"+fn_index+"]");
                 
                        $('body').append(forObject); 
                        var for_left=$(obj).offset().left -(forObject.width()-obj.offsetWidth)/2-getBorderWidth(forObject,"borderLeftWidth");
                        var for_top=$(obj).offset().top -(forObject.height()-obj.offsetHeight)/2-getBorderWidth(forObject,"borderTopWidth");
                        forObject.css({left:for_left,top:for_top});
                         para.animateParamters.left=$(obj).offset().left -(para.options.width-obj.offsetWidth)/2-getBorderWidth(forObject,"borderLeftWidth");
                         para.animateParamters.top=$(obj).offset().top -(para.options.height-obj.offsetHeight)/2-getBorderWidth(forObject,"borderTopWidth");
                         para.animateParamters.width=para.options.width+'px';
                         para.animateParamters.height=para.options.height+'px'; 
                         forObject.html(formatText(para.innerHTML, paraValues));
                   

                        forObject.animate(para.animateParamters,para.animateSpeed,function (){

                            forObject.mouseout(function (event){  
                               var isOut=true;
                               var clientX=event.clientX;
                               var objLeft=forObject.offset().left;
                               var clientY=event.clientY;
                               var objTop=forObject.offset().top;
                               if($.browser.msie){
                                    clientX-=1;
                                    objLeft+=1;
                                    clientY-=1;
                                    objTop+=1
                               }
                                isOut=(clientX<=objLeft||clientX>=objLeft+forObject.width()||clientY<=objTop||clientY>=objTop+forObject.height())
                                $("#info").html(formatText('clientX:{#clientX}         objLeft:{#objLeft}       objWidth:{#objWidth}<br>'
                                +'clientY:{#clientY}              objTop:{#objTop}          topHeight:{#objHeight}<br>border:{#border}',{
                                        clientX:clientX,
                                        objLeft:objLeft,
                                        objWidth:forObject.width(),
                                        clientY:clientY,
                                        objTop:objTop,
                                        objHeight:forObject.height(),
                                        border:getBorderWidth(forObject,"borderLeftWidth")
                                }));
                                if(isOut)
                                    $(this).fadeOut(150,function (){
                                        obj.onmouseover=function (){plugin_mouseover(obj,para)}
                                  
                                    })
                            });
                                      
                           
                            //防止用户在弹出对象变化过程中快速移出鼠标，造成弹出对象不能退出，故做此防范
                           $('body').mousemove(function (event){
                                  var isOut=event.clientX<=forObject.offset().left||event.clientX>=forObject.offset().left+forObject.width()||event.clientY<=forObject.offset().top||event.clientY>=forObject.offset().top+forObject.height()
                
                                  if(isOut){
                                        forObject.hide();
                                        obj.onmouseover=function (){plugin_mouseover(obj,para);};
                                 }
                                 $("#info").html('move'+isOut);
                                $('body').unbind('mousemove');
                            })
                        })
                      
        }
        //插件方法
        $.fn.extend({overShow:function (para){
                this.each(function (fn_index){
                    this.onmouseover=function (){plugin_mouseover(this,para);};
                    this.onmouseout=function (){
                    }
              })
        }})
        
//**************End 详细信息动画*************************************************